package com.example.studentmanager.controller;


import com.example.studentmanager.model.Administrator;
import com.example.studentmanager.service.AdministratorService;
import com.example.studentmanager.service.impl.AdministratorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@RequestMapping("admin")
@Controller
public class AdministratorController {


    @Autowired
    Administrator administrator;

    @Autowired
    AdministratorService administratorService;


    @RequestMapping("/adminAdd")
    public String  addAdministrator(Integer id,String password,String username,String realname){
        id=30;
        username="liu";
        realname="123";
        password="3dd";
        Administrator administrator=new Administrator();
        administrator.setId(id);
        administrator.setPassword(password);
        administrator.setRealname(realname);
        administrator.setUsername(username);
        String result=new AdministratorServiceImpl().saveAdministrator(administrator);
        return result;
    }

    @RequestMapping("/adminSearch")
    public String  searchAdmin(String username){
        username="ldh";
        Administrator administrator=new Administrator();
        administrator=new AdministratorServiceImpl().searchAdminByUserName(username);
        return administrator.toString();
    }


    @RequestMapping("/delAdministrator")
    public String deleteAdmin(String username){
        administrator=new AdministratorServiceImpl().searchAdminByUserName(username);
        if(administrator!=null){
            new AdministratorServiceImpl().deleteAdministrator(administrator);
            return "success";
        }else{
            return "fail";
        }
    }


    @RequestMapping("changeAdmin")
    public String changeAdmin(HttpSession session, Administrator administrator){
        administratorService.changeAdministrator(administrator);
        administrator=administratorService.searchAdminByUserName(administrator.getUsername());
        session.setAttribute("admin",administrator);
        return "back/main/adminInfo";
    }


}
